<?php

class tx_zlib_gateway
{

	/**
	 * The TYPO3 database class
	 *
	 * @var t3lib_DB
	 */
	protected $_db;

	/**
	 * Constructor
	 */
	public function __construct()
	{
		$this->_db = $GLOBALS['TYPO3_DB'];
		$this->init();
	}

    /**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
    public function init()
    {
    }

	/**
	 * @param integer $id
	 * @return tx_zlib_model
	 */
	public function fetch($id)
	{
	}

	/**
	 * @return tx_zlib_collection
	 */
	public function fetchAll()
	{
	}

    /**
     * @param string $table
     * @param string $alias
     * @param boolean $showHidden
     * @return string
     * @see class.tslib_content::enableFields()
     */
    public function enableFields($table, $alias='', $showHidden=false)
    {
		// When TSFE->cObj exists, enableFields is being called from the frontend
		if(isset($GLOBALS['TSFE']->cObj)) {
			$wherePart = $GLOBALS['TSFE']->cObj->enableFields($table, intval($showHidden));
			if(!empty($alias)) {
				$wherePart = str_replace($table, $alias, $wherePart);
			}
			return $wherePart;
		}
		// When TSFE->cObj does not exist, use backend implementation of enableFields
		else {
			return t3lib_BEfunc::BEenableFields($table, $inv = false);
		}
    }

    /**
     * @param string $table
     * @param string $alias
     * @return string
     */
    public function selectFields($table, $alias='')
    {
        $selectPart = '';
        $delim = '';
        $fields = $this->_db->admin_get_fields($table);
        foreach(array_keys($fields) AS $field) {
            if(empty($alias)) {
                $selectPart .= $delim . $field;
            } else {
                $selectPart .= $delim . $alias . '.' . $field . ' AS ' . $alias . '_' . $field;
            }
            $delim = ', ';
        }
        return $selectPart;
    }

    /**
     * @return integer
     */
    public function getLanguageId()
    {
        return intval($GLOBALS['TSFE']->sys_language_uid);
    }

    /**
     * @return integer
     */
    public function getPageId()
    {
        return intval($GLOBALS['TSFE']->page['uid']);
    }

    /**
     * @return integer
     */
    public function getContentId()
    {
        return intval($GLOBALS['TSFE']->cObj->data['uid']);
    }

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_gateway.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_gateway.php']);
}
